package com.eagle.support.utils;


import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import com.eagle.EApplication;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by gc on 2016/12/24.
 */

public class Utils {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param dpValue dp
     * @return px
     */
    public static int dip2px(float dpValue) {
        final float scale = EApplication.getInstance().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static String formatSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.0");
        String fileSizeString = "";
        String wrongSize = "0.0b/s";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "b/s";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "kb/s";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "mb/s";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "gb/s";
        }
        return fileSizeString;
    }

    /**
     * 杀掉后台进程
     *
     * @param context
     */
    public static void killService(Context context) {
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
            String processName = appProcessInfo.processName;
            if (processName.contains("com.kinstalk.voip.sdk.service")) {
                android.os.Process.killProcess(appProcessInfo.pid);
            }
        }

        mActivityManager.killBackgroundProcesses("com.kinstalk.voip.sdk.service");
    }
}
